<?php

namespace App\Models;

class OrderPreset extends BaseModel
{
    protected $table = 'order_preset';

    protected $fillable = [
        'user_id',
        'title',
        'order',
        'sort',
        'is_default',
    ];

    public static $EnumIsDefault = [
        0 => '否', 1 => '是'
    ];

    public function getOrderAttribute($value)
    {
        return json_decode($value, true);
    }

    public function setOrderAttribute($value)
    {
        $this->attributes['order'] = json_encode($value);
    }

    public static function getDefaultSort($user_id){
        $sorts = self::where('user_id', $user_id)->pluck('sort')->toArray();
        $arr = array_diff([0, 1, 2, 3, 4], array_unique($sorts));
        if (count($arr) > 0) {
            return 0;
        } else {
            return array_pop($arr);
        }
    }
}
